<?php

// 平台
class MemberAction extends Action {
	
	private $cLogs = null;
	private $cMember = null;
	

	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.Member');
		import('@.ORG.Plantform');
		import('@.ORG.Logs');
		
		
		$this->cMember 		= new Member();
		$this->cPlantform 	= new Plantform();
		$this->cLogs 		= new Logs();
		
	}
	
	//列表
    public function index(){

    	admin_role('Member:index');

        $get = get(
            array(
                array('begin_time'			,''	,'2014-02-01'),
                array('end_time'			,''	,short_date()),
                array('plantform_id'		,'intval'	,0),
                array('search'			    ,'trim'	,1),
                array('keywords'		    ,'trim'	,''),
                array('p'				    ,'intval'	,1),
            )
        );

        //开始时间
        $begin_time	= $get['begin_time'];
        //结束时间
        $end_time	= $get['end_time'];

        //平台
        $plantform_id= $get['plantform_id'];

        //条件
        $search     = $get['search'];
        //关键词
        $keywords   = $get['keywords'];

    	$page   	= $get['p'];
    	$pageSize 	= 30;


        //平台
        $plantforms = $this->cPlantform->lists();

        //查询条件
        $member_search = C('MEMBER_SEARCH');

        $where = array(
            'plantform_id'  => $plantform_id,
            'begin_time'    => $begin_time,
            'end_time'      => $end_time,
            'search'        => array('item'=>$search,'value'=>$keywords)
        );

    	$allCount = 0;
    	
    	$members = $this->cMember->lists($where,$page,$pageSize,$allCount);
       
    	/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
		//导入分页类
		import("@.ORG.Page");
		
		$pageParams = array(
			'g'		=> 'Admin',
			'begin' => $begin,
			'end'	=> $end,
			'search' => $search,
			'keywords' => $keywords
		);
		 // 实例化分页类
	  	$p = new Page($allCount,$pageSize,$pageParams);
	  	
    	// 分页显示输出
    	$pager = $p->show();

		/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'members'		=> $members,
    			'pager'			=> $pager,
                'begin_time'	=> $begin_time,
                'end_time'		=> $end_time,
                'plantform_id'	=> $plantform_id,
                'plantforms'    => $plantforms,
                'member_search' => $member_search,
                'search'        => $search,
                'keywords'      => $keywords,
    			'MainPage'		=> 'Member:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

    	admin_role('Member:add');
    	
    	$plantforms = $this->cPlantform->lists();
    	
    	
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'plantforms'=> $plantforms,
    			
    			'MainPage'	=> 'Member:add'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){

    	admin_role('Member:add');
    	
   		$post = post(array(
    		array('member_id'		,'intval','0'),
    		array('member_name'		,'trim',''),
    		array('member_pwd'		,'trim',''),
    		array('member_pay_pwd'	,'trim',''),
    		array('mobile_phone'	,'trim',''),
    		array('email'			,'trim',''),
    		array('email_pwd'		,'trim',''),   		
    		array('plantform_id'	,'intval',''),
    		array('disabled'		,'intval',''),
    		array('is_internal'		,'intval','0'),
    		array('remark'			,'trim','')
    	));

    	$post['admin_id'] = admin_id_ajax();
    	
    	
    	$where = array(
    		'plantform_id' 	=> $post['plantform_id'],
    		'member_name'	=> $post['member_name']
    	);
    	
    	//已存在
    	$exist_member = $this->cMember->exist($where);
    	
    	if(!empty($exist_member)){
    		json_error('会员名在该平台已存在');
    	}
    	 
    	$add_result = $this->cMember->add($post);
    		
		if($add_result){
			
			$this->cLogs->add('Member:add','会员添加成功:'.$post['member_name']);
			
			json_success('会员添加成功');
		}else{
			
			$this->cLogs->add('Member:add','会员添加失败:'.$post['member_name']);
			
			json_error('会员添加失败');
		}
    }
    
    
 	//批量添加
    public function import_add_do(){
    	
    	admin_role('Member:add');
    	
    	import('@.ORG.FileCsvUpload');
		import('@.ORG.MemberCsv');
    	
    	
       	$cfcUP = new FileCsvUpload('member');
    	$up_result = $cfcUP->upload();
		    	
    	if(is_null($up_result)){
    		//不是上传
    		
    		redirect('/index.php/Admin/Member/add',3,'请先上传文件');
    		
    	}else if($up_result == false){
    		//上传错误
    		$up_error = $cfcUP->file_error();
    		
    		redirect('/index.php/Admin/Member/add',3,$up_error);
    		
    	}else{
    		$up_success	= 1;
    		
    		$cfcUP->remove_header();
    		$file_data = $cfcUP->file_data();
    		
    		//转换
    		$cMCsv = new MemberCsv();
    		$members = $cMCsv->array_to_data($file_data);
    					
			//增加平台映射
			$members = $this->cMember->append_plantform_id($members);
			
			//有几个没有匹配到平台
			$mError 	= 0;
			$mErrorMsg 	= '';
			$admin_id 	= admin_id_ajax();
			
			foreach ($members as $i => $m){
				if($m['plantform_id'] == '0'){
					$mError++;
					$mErrorMsg 	.= ($i+1).'行,';
				}
				
				$members[$i]['admin_id'] = $admin_id;
			}
			
			if($mError > 0){
				$mErrorMsg 	.= '没有匹配到平台信息，请检查';
				
				redirect('/index.php/Admin/Member/add',3,$mErrorMsg);
			}
    	}

    	$add_count 		= 0;
    	$update_count 	= 0;

    	foreach ($members as $i => $m){
    		//添加入库
    		
    		$where = array(
    			'plantform_id' 	=> $m['plantform_id'],
    			'member_name'	=> $m['member_name']
	    	);
	    	
	    	//已存在
	    	$exist_member = $this->cMember->exist($where);
    	
    		if(!empty($exist_member)){

                //用户已存在，则更新
                $id = $exist_member['member_id'];
                $aft_row = $this->cMember->update($id,$m);

                if($aft_row){
                    $update_count++;
                }
    			continue;
    		}
	    	
    		$add_id = $this->cMember->add($m);
    		if($add_id > 0){
    			$add_count++;
    		}
    	}
    	
    	$exist_msg = '';
    	if($update_count > 0){
    		$exist_msg = $update_count.'个会员已更新';
    	}
    	
    	$this->cLogs->add('Member:add','会员添加成功:共'.$add_count.'条数据'.$exist_msg);
    	
    	redirect('/index.php/Admin/Member/index',3,'共'.$add_count.'条数据,添加成功!'.$exist_msg);
    }
    
    
    
	//批量修改
    public function import_update_do(){
    	
    	admin_role('Member:edit');
    	
    	import('@.ORG.FileCsvUpload');
		import('@.ORG.MemberCsv');
    	
    	
       	$cfcUP = new FileCsvUpload('member');
    	$up_result = $cfcUP->upload();
		    	
    	if(is_null($up_result)){
    		//不是上传
    		
    		redirect('/index.php/Admin/Member/add',3,'请先上传文件');
    		
    	}else if($up_result == false){
    		//上传错误
    		$up_error = $cfcUP->file_error();
    		
    		redirect('/index.php/Admin/Member/add',3,$up_error);
    		
    	}else{
    		$up_success	= 1;
    		
    		$cfcUP->remove_header();
    		$file_data = $cfcUP->file_data();
    		
    		//转换
    		$cMCsv = new MemberCsv();
    		$members = $cMCsv->update_array_to_data($file_data);

    	}

    	$up_count = 0;
    	foreach ($members as $i => $m){
    		//添加入库    		
    		$id = $m['member_id'];
    		unset($m['member_id']);
    		$add_id = $this->cMember->update($id,$m);
    		if($add_id > 0){
    			$up_count++;
    		}
    	}
    	
    	$this->cLogs->add('Member:edit','会员更新成功:共'.$up_count.'条数据');
    	
    	redirect('/index.php/Admin/Member/index',2,'共'.$up_count.'条数据,更新成功!');
    }
    
    
    //批量添加
    public function batch_add_do(){
    	
    	admin_role('Member:add');
    	
    	import('@.ORG.BatchAdd');
    	import('@.ORG.MemberBatchAdd');
    	

   		$post = post(array(
    		array('content'	,'trim','')
    	));

    	
    	$post['admin_id'] 	= admin_id_ajax();
    	$content			= $post['content'];
    	
    	
    	$cMBatchAdd = new MemberBatchAdd();
    	$aly_result = $cMBatchAdd->analyze($content);
    	
    	if($aly_result){
    		//正常
    		$check_rlt = $cMBatchAdd->format_check();
    		
    		if($check_rlt){
    			//正确
    			
    			//格式化
    			$members = $cMBatchAdd->line_format();
    			
    			$add_count = 0;
    			foreach ($members as $i => $m){
    				//添加入库
    				
    				$add_id = $this->cMember->add($m);
    				if($add_id > 0){
    					$add_count++;
    				}
    			}
    			
    			$this->cLogs->add('Member:add','会员添加成功:共'.$add_count.'条数据');
    			
    			json_success('共'.$add_count.'条数据,添加成功!');
    		}else{
    			//有错
    			json_error('数据格式检查有错误',$cMBatchAdd->format_errors);
    		}
  
    	}else{
    		//错误 
    		json_error($cMBatchAdd->error,$cMBatchAdd->format_errors);
    	}
    }
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('Member:edit');
    	
    	$get = get(array(
    		array('id','intval',0)
    		
    	));

    	$id = $get['id'];
    	
    	//会员
    	$member = $this->cMember->get_by_member_id($id);
    	
    	//平台
    	$plantforms = $this->cPlantform->lists();
    	    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'member'		=> $member,
    			'plantforms'	=> $plantforms,    			
    			'MainPage'		=> 'Member:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('Member:edit');
    	 
		$post = post(array(
    		array('member_id'		,'intval','0'),
    		array('member_name'		,'trim',''),
    		array('member_pwd'		,'trim',''),
    		array('member_pay_pwd'	,'trim',''),
			array('mobile_phone'	,'trim',''),
    		array('email'			,'trim',''),
    		array('email_pwd'		,'trim',''),
    		array('plantform_id'	,'intval',''),
    		array('disabled'		,'intval',''),
    		array('is_internal'		,'intval','0'),
    		array('remark'			,'trim',''),
    		array('last_use_time'	,'','')
    	));
   	
    	$id = $post['member_id'];
    	
    	unset($post['member_id']);
    	
    	$update_result = $this->cMember->update($id,$post);
    	
    	if($update_result){
    		
    		$this->cLogs->add('Member:edit','会员修改成功:'.$post['member_name']);
    		
			json_success('保存成功');
		}else{
			json_error('保存失败');
		}
    }
    
    
 	public function export_csv(){
    
    	admin_role('Member:index');
    	
    	$file_name = '帐户列表 '.date('Y-m-d').'.csv';
    	
    	//导出的订单
    	$data = $this->cMember->export_members();

    	export_csv($data,$file_name);
    }
    
}